package com.group.flagtivities;

import java.util.Timer;
import java.util.TimerTask;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

public class Welcome extends Activity implements OnClickListener {
	
	private static LinearLayout login;
	private static EditText username;
	private static EditText password;
	private static Button button;
	private static RelativeLayout buttonContainer;
	private static ProgressBar buttonIndicator;
	
	private static int user_id;
	private static String name;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.welcome);
		
		SharedPreferences settings = getSharedPreferences("flagtivitiesPrefs", MODE_PRIVATE);
		user_id = settings.getInt("user_id", 0);
		name = settings.getString("name", null);
		
		findViews();
		
		TextUtil.setFont((ViewGroup) findViewById(R.id.welcome), getAssets());
	}
	
	private void findViews() {
		login = (LinearLayout) findViewById(R.id.login);
		username = (EditText) findViewById(R.id.username);
		password = (EditText) findViewById(R.id.password);
		button = (Button) findViewById(R.id.button);
		buttonContainer = (RelativeLayout) findViewById(R.id.buttonContainer);
		buttonIndicator = (ProgressBar) findViewById(R.id.buttonIndicator);
	}
	
	private void shake() {
		Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
		login.startAnimation(shake);
	}
	
	private void setInteractable(boolean isInteractable) {
		buttonIndicator.setVisibility(isInteractable ? View.GONE : View.VISIBLE);
		username.setEnabled(isInteractable ? true : false);
		password.setEnabled(isInteractable ? true : false);
		login.setEnabled(isInteractable ? true : false);
		button.setEnabled(isInteractable ? true : false);
	}
	
	private void validateLogin() {
		final String mUsername = username.getText().toString().trim();
		final String mPassword = password.getText().toString().trim();
		
		username.setText(mUsername);
		password.setText(mPassword);
		
		if (mUsername.isEmpty() || mPassword.isEmpty()) {
			shake();
		}else {
			setInteractable(false);
			Timer t = new Timer();
			t.schedule(new TimerTask() {
				public void run() {
					String response = WebUtil.login(mUsername, mPassword);
					int user_id = 0;
					String name = null;
					try{
						JSONArray jLoginArray = new JSONArray(response);
						JSONObject jLogin = jLoginArray.getJSONObject(0);
						user_id = Integer.parseInt(jLogin.getString("USER_ID"));
						name = jLogin.getString("FIRST") + " " + jLogin.getString("LAST");
					}catch(Exception e) {
						user_id = 0;
						name = null;
					}
					if (response == null || user_id == 0) {
						runOnUiThread(new Runnable() {
							public void run() {
								setInteractable(true);
								shake();
								password.setText("");
								password.requestFocus();
							}
						});
					}else {
						login(user_id, name);
						
					}
				}
			}, 1000);
		}
	}
	
	private void login(int user_id, String name) {
		SharedPreferences settings = getSharedPreferences("flagtivitiesPrefs", MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		
		editor.putInt("user_id", user_id);
		editor.putString("name", name);
		editor.commit();
		
		Intent i = new Intent(this, Flagtivities.class);
		i.putExtra("user_id", user_id);
		i.putExtra("name", name);
		startActivity(i);
		finish();
	}
	
	@Override
	public void onStart() {
		super.onStart();
		
		Timer t = new Timer();
		t.schedule(new TimerTask() {
			public void run() {
				if (user_id == 0) {
					runOnUiThread(new Runnable() {
						public void run() {
						login.setVisibility(View.VISIBLE);
						buttonContainer.setVisibility(View.VISIBLE);
						username.requestFocus();
						}
					});
				}else {
					Intent i = new Intent(Welcome.this, Flagtivities.class);
					i.putExtra("user_id", user_id);
					i.putExtra("name", name);
					startActivity(i);
					finish();
				}
			}
		}, 3000);
	}

	@Override
	public void onClick(View v) {
		validateLogin();
	}

}
